﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using DomesticPestLib;
using System.Collections;

namespace DomesticPestProject
{
    public partial class frmLoc : System.Web.UI.Page
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader reader;
        //EntyLayer enty;
        DLayer dlayer;
        ArrayList objalst;
        entyLoc enty;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //btnCommodity.Enabled = false;
                btnLctn.Enabled = false;
                BindDetails();
            }
        }
        private void BindDetails()
        {

            {
                enty = new entyLoc();
                dlayer = new DLayer();
                objalst = new ArrayList();
                objalst = dlayer.selectLoc();
                gvLoc.DataSource = objalst;
                gvLoc.DataBind();
                //DataList1.DataSource = objalst;
                //DataList1.DataBind();
            }

        }
        private void clear()
        {
            txtCounCd.Text = "";
            txtCoun.Text = "";
            txtGeoRgn.Text = "";
            txtGIS.Text = "";
            txtLoc.Text = "";
            txtState.Text = "";
        }

        void connect()
        {
            conn = new SqlConnection();
            conn.ConnectionString = "Data Source=KAJAL-PC;Initial Catalog=DomesticPestDB;Integrated Security=True";
            conn.Open();
            cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
        }
        void disconnect()
        {
            //cmd.Dispose();
            //if (reader != null)
            //    reader.Close();
            //if (conn != null)
            //    conn.Close();
        }

        protected void btnCommodity_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmCommodity.aspx");
        }

        protected void btnInformation_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmInformation.aspx");
        }

        protected void btnTransaction_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmTransaction.aspx");
        }

        protected void btnPest_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPest.aspx");
        }

        protected void btnLctn_Click(object sender, EventArgs e)
        {
           // Response.Redirect("");
        }

        protected void btnPsDsrbtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPestDistribution.aspx");
        }

        protected void btnMain_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmMain.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                connect();
                
                cmd.CommandText = "INSERT INTO Country values('" + txtCounCd.Text + "','" + txtGeoRgn.Text + "','" + txtCoun.Text  + "','" + txtState.Text + "','" + txtLoc.Text + "','" + txtGIS.Text + "')";

                cmd.ExecuteNonQuery();
                //mbox "";
                disconnect();
            }
            catch (Exception ex)
            {
                if (conn != null)
                    conn.Close();
            }
            disconnect();
            BindDetails();
            clear();
            //btnAdd.Enabled = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnl1.Visible = false;
            pnlAdd.Visible = true;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                connect();
                //string strradio = "";
                //if (rdFemale.Checked == true)
                //{
                //    strradio = rdFemale.Text;
                //}
                //else if(rdMale.Checked==true)
                //{
                //strradio=rdMale.Text;
                //}
                //int next = NextId();
                cmd.CommandText = "delete * from Country where Country_Code='" + txtCounCd + "'";

                cmd.ExecuteNonQuery();
                //mbox "";
                disconnect();
            }
            catch (Exception ex)
            {
                if (conn != null)
                    conn.Close();
            }
            disconnect();
        }


        protected void gvLoc_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvLoc.EditIndex = e.NewEditIndex;
            BindDetails();
        }

        protected void gvLoc_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
             gvLoc.EditIndex = -1;
             BindDetails();
        }

        protected void gvLoc_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            entyLoc loc = new entyLoc();
            TextBox txtCounCd = gvLoc.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            TextBox txtCoun = gvLoc.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            TextBox txtGeoRgn = gvLoc.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
            TextBox txtState = gvLoc.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
            TextBox txtLoc = gvLoc.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
            TextBox txtGIS = gvLoc.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;
            loc.Code = txtCounCd.Text;
            loc.Name = txtCoun.Text;
            loc.GeoRegion = txtGeoRgn.Text;
            loc.State = txtState.Text;
            loc.Location = txtLoc.Text;
            loc.GIS = txtGIS.Text;
           
            DLayer d = new DLayer();
            (new DLayer()).UpdateLoc(loc);
            gvLoc.EditIndex = -1;
            BindDetails();
            



        }

        protected void gvLoc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string loccode = gvLoc.Rows[e.RowIndex].Cells[2].Text;
            (new DLayer()).DeleteLoc(loccode);
            BindDetails();
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            ddlCountCd.Visible = true;
            txtCounCd.Visible = false;
        }

        protected void ddlCountCd_SelectedIndexChanged(object sender, EventArgs e)
        {
            //entyhome = new EntyHome();
            //clshome = new clsHome();
            //objalst = new ArrayList();
            //objalst = clshome.selectSubCtgryByID(2);
            //DataList1.DataSource = objalst;
            //DataList1.DataBind();
            //Menu1.Items[1].Selected = true;
            //Menu1.Items[0].ImageUrl = "~/Images/CategoryDisabled.png";
            //Menu1.Items[1].ImageUrl = "~/Images/SubCategoryEnabled.png";
            //MultiView1.ActiveViewIndex = 1;

            enty = new entyLoc();
            dlayer = new DLayer();
            objalst = new ArrayList();
            objalst = dlayer.selectByCoutryCd(ddlCountCd.Text);
            txtCounCd.Text = enty.Code;



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
           
        }
    }
}